# coding=UTF-8
import os
from utils import read_config, date_util, doc_util
from database.mapper import stock_month_macd, monitor_result, stock_info

base_path = read_config.base_path
config_path = os.path.join(read_config.data_path, 'common', 'config')


# 月 macd 上穿0轴
def month_cross(c_list):
    deal_date = date_util.find_last_valid_day()
    m_list = monitor_result.select_date_type(deal_date, 2)
    m_dict = {}
    for info in m_list:
        m_dict[info['code']] = info
    if len(c_list) == 0:
        c_list = stock_info.selectall()
    for stock in c_list:
        code = stock['code']
        # 市值<50
        if code in m_dict.keys() or int(stock['total_val']) < 80:  # 已经完成
            print(code + '市值太小')
            continue
        macd_list = stock_month_macd.select_newest_size(code, 3)
        if len(macd_list) < 3:
            print(code + '这支不太行')
            continue
        if date_util.diff_days_str(date_util.date_to_str(macd_list[0]['rq'], '%Y-%m-%d'), deal_date) != 0:
            print(code + '不是最新的月macd不计算')
        macd0 = macd_list[0]['macd']
        macd1 = macd_list[1]['macd']
        macd2 = macd_list[2]['macd']
        if macd0 > 0 and macd1 > 0:
            continue
        monitor_name = ''
        if macd0 >= 0 and macd1 < 0:
            monitor_name = '已经突破'
        if macd2 < macd1 < macd0 < 0 and macd0 > -0.01:
            monitor_name = '即将突破'
        if monitor_name == '':
            continue
        row_info = []
        row_info.append(code)
        row_info.append(stock['name'])
        row_info.append(round(macd2, 4))
        row_info.append(round(macd1, 4))
        row_info.append(round(macd0, 4))
        content_arr = [['编码', '名称', '上上月macd', '上月macd', '本月macd']]
        content_arr.append(row_info)
        monitor_data = {'code': code, 'monitor_date': deal_date,
                        'monitor_name': monitor_name, 'monitor_type': 2,
                        'monitor_result': {'content_arr': str(content_arr)}}
        monitor_result.insert(monitor_data)
